package com.xhsj.user.pms.workflow.service.impl;


import com.xhsj.user.pms.common.constant.IdentifyConstant;
import com.xhsj.user.pms.workflow.domain.WorkflowComplete;
import com.xhsj.user.pms.workflow.domain.WorkflowIdentitylink;
import com.xhsj.user.pms.workflow.dto.ExecProgressDto;
import com.xhsj.user.pms.workflow.mapper.WorkflowCompleteMapper;
import com.xhsj.user.pms.workflow.mapper.WorkflowIdentitylinkMapper;
import com.xhsj.user.pms.workflow.service.WorkflowIdentityLinkService;
import com.xhsj.user.utils.IdGen;
import com.xhsj.user.utils.Message;
import com.xhsj.user.utils.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.util.Date;
import java.util.List;

/**
 * @Description:
 * @Auther: 罗季嘉
 * @Date: 2020/1/17 11:03
 */
@Slf4j
@Service
@Transactional(rollbackFor = Exception.class)
public class WorkflowIdentityLinkServiceImpl implements WorkflowIdentityLinkService {

    @Autowired
    private WorkflowIdentitylinkMapper workflowIdentitylinkMapper;

    @Autowired
    private WorkflowCompleteMapper workflowCompleteMapper;


    //修改当前流程指派人的所在的状态
    @Override
    public int updateIdentityLinkStatusId(String identitylinkId, Integer jumpForm) {
        WorkflowIdentitylink identitylink = new WorkflowIdentitylink();
        identitylink.setId(identitylinkId);
        identitylink.setJumpForm(jumpForm);
        return workflowIdentitylinkMapper.updateById(identitylink);
    }

    /**
     * 根据专利ID,节点ID查询对应节点执行人
     * @param nodeId
     *  label 标识:user为用户,role添加至角色,dept添加至部门
     */
    @Override
    public void setProgressIdentify2(ExecProgressDto param, String nodeId, String nodeName) throws Exception {
        try {
            Date curentDate=new Date(System.currentTimeMillis()+1000);
            List<Long> users = param.getUsers();
            if (users != null && users.size() >0) {
                for(Long user : param.getUsers()) {
                    WorkflowIdentitylink identitylink = new WorkflowIdentitylink();
                    identitylink.setId(IdGen.generateUUID());
                    BeanUtils.copyProperties(param,identitylink);
                    identitylink.setNodeName(nodeName);
                    identitylink.setNodeId(nodeId);
                    identitylink.setExamineStatus(0);
                    identitylink.setCreateTime(curentDate);
                    switch (param.getLabel()) {
                        case IdentifyConstant.USER:
                            identitylink.setIdentitylinkUserId(Long.valueOf(user));
                            break;
                        case IdentifyConstant.ROLE:
                            identitylink.setIdentitylinkGroupId(Long.valueOf(user));
                            break;
                        case IdentifyConstant.DEPT:
                            identitylink.setIdentitylinkDeptId(Long.valueOf(user));
                            break;
                    }
                    if(StringUtils.isNotNull(identitylink.getIdentitylinkUserId()) ||
                            StringUtils.isNotNull(identitylink.getIdentitylinkGroupId()) ||
                            StringUtils.isNotNull(identitylink.getIdentitylinkDeptId())) {
                        int i1 = workflowIdentitylinkMapper.insert(identitylink);
                        if (i1 != 1) {
                            throw new Exception("保存下一个节点执行人异常");
                        }
                        WorkflowComplete workflowComplete = new WorkflowComplete();
                        BeanUtils.copyProperties(identitylink,workflowComplete);
                        workflowComplete.setUpdateTime(curentDate);
                        workflowComplete.setDealUser(String.valueOf(user));
                        workflowComplete.setLabel(param.getLabel());
                        workflowComplete.setDealUserId(user);
                        int i = workflowCompleteMapper.insert(workflowComplete);
                        if (i != 1) {
                            throw new Exception("保存历史表异常");
                        }
                    }
                }
            }

        } catch (Exception e) {
            log.error("根据专利ID,节点ID查询对应节点执行人,{}",e);
            throw e;
        }
    }


    /**
     * 根据专利ID,节点ID查询对应节点执行人
     * @param nodeId
     *  label 标识:user为用户,role添加至角色,dept添加至部门
     */
    @Override
    public Message setProgressIdentify(ExecProgressDto param, String nodeId, String nodeName) {
        try {
                   Date curentDate=new Date();
                for(Long user : param.getUsers()) {
                    WorkflowIdentitylink identitylink = new WorkflowIdentitylink();
                    identitylink.setId(IdGen.generateUUID());
                    BeanUtils.copyProperties(param,identitylink);
                    identitylink.setNodeName(nodeName);
                    identitylink.setNodeId(nodeId);
                    identitylink.setExamineStatus(0);
                    identitylink.setCreateTime(curentDate);
                    switch (param.getLabel()) {
                        case IdentifyConstant.USER:
                            identitylink.setIdentitylinkUserId(Long.valueOf(user));
                            break;
                        case IdentifyConstant.ROLE:
                            identitylink.setIdentitylinkGroupId(Long.valueOf(user));
                            break;
                        case IdentifyConstant.DEPT:
                            identitylink.setIdentitylinkDeptId(Long.valueOf(user));
                            break;
                    }
                    if(StringUtils.isNotNull(identitylink.getIdentitylinkUserId()) ||
                            StringUtils.isNotNull(identitylink.getIdentitylinkGroupId()) ||
                            StringUtils.isNotNull(identitylink.getIdentitylinkDeptId())) {
                        int i1 = workflowIdentitylinkMapper.insert(identitylink);
                        if (i1 != 1) {
                            throw new Exception("保存下一个节点执行人异常");
                        }
                        WorkflowComplete workflowComplete = new WorkflowComplete();
                        BeanUtils.copyProperties(identitylink,workflowComplete);
                        workflowComplete.setUpdateTime(curentDate);
                        workflowComplete.setDealUser(String.valueOf(user));
                        workflowComplete.setLabel(param.getLabel());
                        int i = workflowCompleteMapper.insert(workflowComplete);
                        if (i != 1) {
                            throw new Exception("保存历史表异常");
                        }
                    }
                }
        } catch (Exception e) {
            log.error("根据专利ID,节点ID查询对应节点执行人,{}",e);
        }
        return null;
    }

}
